import { configureStore } from "@reduxjs/toolkit";
import { useSelector, TypedUseSelectorHook } from "react-redux";

import userSlice from "./reducers/user";

const _store = configureStore({
  reducer: {
    user: userSlice.reducer,
  },
});

const actions = {
  ...userSlice.actions,
};

export type ActionsKey = keyof typeof actions;

export type RootState = ReturnType<typeof store.getState>;

// TODO: T 可能不合适
export const store = {
  ..._store,
  _dispatch: <T>(key: ActionsKey, val: T) => {
    _store.dispatch(actions[key](val));
  },
};

export const useStoreSelector: TypedUseSelectorHook<RootState> = useSelector;
